'''
Created on 2009-03-11

@author: belleyma
'''
from tkinter import *

class Rules(Frame):
    '''
    classdocs
    '''

    def __init__(self, master):
        '''
        Constructor
        '''
        Frame.__init__(self, master)
        self.pack()
        self.body()
        
    def body(self):
        
        sl = ScrolledList(self)
        sl.grid(row=0, rowspan=3, sticky=W)
        Rules.__sl = sl
        btnAdd = Button(self, text="Add", command=Rules.addRule, width=10)
        btnAdd.grid(row=0, column=1, sticky=W)
        btnDelete = Button(self, text="Delete", width=10)
        btnDelete.grid(row=1, column=1, sticky=W)
        btnModify = Button(self, text="Modify", command=Rules.modifyRule, width=10)
        btnModify.grid(row=2, column=1, sticky=W)

    def addRule():
        sl = Rules.__sl
        sl.addListItem("New rule " + str(sl.getSize()))
                        
    def deleteRule():
        sl = Rules.__sl
        print ("Delete rule! ", str())
        
    def modifyRule():
        print ("Modify rule!")
        

class ScrolledList(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.pack(expand=YES, fill=BOTH)                  
        self.makeWidgets()
        
    def handleList(self, event):
        index = self.listbox.curselection()            
        label = self.listbox.get(index)                
        self.runCommand(label)     
                            
    def addListItem(self, item):
         self.listbox.insert(END, item)              

    def getSize(self):
         return self.listbox.size()             

    def makeWidgets(self):                    
        sbar = Scrollbar(self)
        list = Listbox(self, relief=SUNKEN, width=70)
        sbar.config(command=list.yview)                
        list.config(yscrollcommand=sbar.set)              
        sbar.pack(side=RIGHT, fill=Y)                     
        list.pack(side=LEFT, expand=YES, fill=BOTH)    
 
        list.bind('<Double-1>', self.handleList)       
        self.listbox = list
        
    def runCommand(self, selection):                   
        print ('You selected:', selection)

if __name__ == '__main__':
    options = map((lambda x: 'Lumberjack-' + str(x)), range(20))
    ScrolledList().mainloop()
        